package com.lei.dao.impl;

import com.lei.dao.MenuDao;
import com.lei.entity.Menu;
import com.lei.util.BaseDb;
import com.lei.util.TransBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/16 20:18
 */
public class MenuDaoImpl implements MenuDao {
    @Override
    public List<Menu> getAllMenuByRid(Integer rid) {
        //查找所有的父权限
        String sql = "SELECT m.mid,m.mname,m.mcode,m.murl,m.mpid,m.micon FROM t_role_premission r JOIN t_menu m ON r.rid = ? AND r.mid = m.mid AND m.mpid = 0";
        List<Map> maps = BaseDb.query(sql, rid);
        //获得所有的父权限
        List<Menu> fatherList = new ArrayList();
        if (maps.size() > 0) {
            fatherList = TransBean.populate(Menu.class, maps);
        }

        //查找所有的子权限
        sql = "SELECT m.mid,m.mname,m.mcode,m.murl,m.mpid,m.micon FROM t_role_premission r JOIN t_menu m ON r.rid = ? AND m.mpid = ? AND r.mid = m.mid ";
        for (Menu menu : fatherList) {
            List<Map> sonList = BaseDb.query(sql, rid, menu.getMid());
            if (sonList.size() > 0) {
                menu.setSonMenu(TransBean.populate(Menu.class, sonList));
            }
        }
        return fatherList;
    }

    @Override
    public List<Menu> getAllMenu() {
        List<Menu> fatherList = new ArrayList();
        //查找所有的父权限
        String sql = "select mid,mname from t_menu where mpid = 0";
        List<Map> maps = BaseDb.query(sql);
        if (maps.size() > 0) {
            //获得所有的父权限
            fatherList = TransBean.populate(Menu.class, maps);
            for (Menu menu : fatherList) {
                //查找所有的子权限
                sql = "select mid,mname from t_menu where mpid = ? ";
                List<Map> sonMaps = BaseDb.query(sql, menu.getMid());
                if (sonMaps.size() > 0) {
                    //获得所有的子权限
                    menu.setSonMenu(TransBean.populate(Menu.class, sonMaps));
                }
            }
        }
        return fatherList;
    }

    @Override
    public List<Integer> getMenuByRid(String rid) {
        ArrayList<Integer> list = new ArrayList<>();
        String sql = "select mid from t_role_premission where rid = ?";
        List<Map> maps = BaseDb.query(sql, rid);
        if (maps.size() > 0) {
            for (Map map : maps) {
                // 将查询到的mid装填到list中
                list.add((Integer) map.get("mid"));
            }
        }
        return list;
    }

    @Override
    public String updateRoleMenu(String rid, String[] mids) {
        try {
            String sql = "delete from t_role_premission where rid = ?";
            BaseDb.update(sql, rid);
            sql = "insert into t_role_premission (id,rid,mid) values (null,?,?)";
            if (mids != null) {
                for (String mid : mids) {
                    BaseDb.update(sql, rid, mid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 返回 0 修改失败
            return "0";
        }
        // 返回 1 修改成功
        return "1";
    }
}
